package com.mdd.front.controller.yangyangLeadingEnterprise.personalCenter;

import com.alibaba.fastjson.JSONObject;
import com.mdd.common.aop.NotLogin;
import com.mdd.common.entity.yangyangLeadingEnterprise.TAppEnterprise;
import com.mdd.common.entity.yangyangLeadingEnterprise.common.JsonReturn;
import com.mdd.common.entity.yangyangLeadingEnterprise.param.CommonParam;
import com.mdd.common.entity.yangyangLeadingEnterprise.param.EnterpriseUserParam;
import com.mdd.common.entity.yangyangLeadingEnterprise.param.SwitchingCompanyParam;
import com.mdd.common.util.RedisUtils;
import com.mdd.front.service.yangyangLeadingEnterprise.ITAppEnterpriseService;
import com.mdd.front.service.yangyangLeadingEnterprise.ITAppEnterpriseUserService;
import com.mdd.front.service.yangyangLeadingEnterprise.ITAppUserDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

@RestController
@RequestMapping("api/personalCenter/enterprise")
@Api(tags = "企业信息")
public class AppEnterpriseController {

    @Resource
    ITAppEnterpriseService appEnterpriseService;

    @Resource
    ITAppEnterpriseUserService appEnterpriseUserService;

    @Resource
    ITAppUserDataService appUserDataService;

    /**
     * 企业信息详情
     * @param param
     * @return
     */
    @PostMapping(value = "/selectEnterpriseById",produces = "application/json; charset=UTF-8")
    @ApiOperation(value="企业信息详情")
    public String selectEnterpriseById(@RequestBody CommonParam param) {
        Integer id = param.getUserId();
        if(id == null) {
            return JsonReturn.fail("必传参数不能为空", 40001).toJsonStr();
        }
        TAppEnterprise appEnterprise = appEnterpriseService.selectEnterpriseById(id);
        if(appEnterprise == null || appEnterprise.getId() == null){
            return JsonReturn.fail("企业信息不存在", 40002).toJsonStr();
        }
        return JsonReturn.success().put("appEnterprise",appEnterprise).toJsonStr();
    }

    /**
     * 用户企业认证---验证手机号
     * @param param
     * @return
     */
    @NotLogin
    @PostMapping(value = "/enterpriseCertificationCheckPhone",produces = "application/json; charset=UTF-8")
    @ApiOperation(value="用户企业认证---验证手机号")
    public String enterpriseCertificationCheckPhone(@RequestBody EnterpriseUserParam param, HttpServletRequest request) {
        String phone = param.getPhone();
        String code = param.getCode();
        if(phone == null || phone.isEmpty() || code == null || code.isEmpty()) {
            return JsonReturn.fail("必传参数不能为空！", 40001).toJsonStr();
        }
        if(!RedisUtils.exists(phone)){
            return JsonReturn.fail("验证码已过期",40002).toJsonStr();
        }
        Object codeKeyData =  RedisUtils.get(phone);
        if(codeKeyData==null){
            return JsonReturn.fail("验证码已失效",40003).toJsonStr();
        }
        if(code.equals(codeKeyData)){
            RedisUtils.del(phone);
            return JsonReturn.success().toJsonStr();
        }
        return JsonReturn.fail().toJsonStr();
    }

    /**
     * 用户（法人）绑定企业信息
     * @param param
     * @return
     */
    @NotLogin
    @PostMapping(value = "/insertEnterpriseUser",produces = "application/json; charset=UTF-8")
    @ApiOperation(value="用户（法人）绑定企业信息")
    public String insertEnterpriseUser(@RequestBody EnterpriseUserParam param,HttpServletRequest request) {
        String token = request.getHeader("token");
        param.setToken(token);
        return appEnterpriseUserService.insertEnterpriseUser(param);
    }

    /**
     * 查询用户名下企业/用户企业下拉列表
     * @param param
     * @return
     */
    @PostMapping(value = "/selectEnterpriseListByUserId",produces = "application/json; charset=UTF-8")
    @ApiOperation(value="查询用户名下企业/用户企业下拉列表")
    public String selectEnterpriseListByUserId(@RequestBody CommonParam param) {
        Integer userId = param.getUserId();
        if(userId == null) {
            return JsonReturn.fail("必传参数不能为空",40001).toJsonStr();
        }
        List<TAppEnterprise> appEnterpriseList = appEnterpriseService.selectEnterpriseListByUserId(userId);
        return JsonReturn.success().setListData(Optional.ofNullable(appEnterpriseList).orElse(new ArrayList<>())).toJsonStr();
    }

    /**
     * 用户切换企业调用接口
     * @param
     * @return
     */
    @NotLogin
    @PostMapping(value = "/switchingCompanies",produces = "application/json; charset=UTF-8")
    @ApiOperation(value="用户切换企业调用接口")
    public String switchingCompanies(@RequestBody SwitchingCompanyParam param) {
        return appUserDataService.switchingCompanies(param.getUserDataId(),param.getEnterpriseId());
    }


//    public static void main(String[] args) {
//        Map<String, String> map = new HashMap<>();
//        map.put("zimVer", "3.0.0");
//        map.put("appVersion", "1");
//        map.put("bioMetaInfo", "4.1.0:1150****,0");
//        map.put("appName", "com.aliyun.antcloudauth");
//        map.put("deviceType", "ios");
//        map.put("osVersion", "iOS 10.3.2");
//        map.put("apdidToken", "");
//        map.put("deviceModel", "iPhone9,1");
//        JSONObject.toJSON(map).toString();
//        System.out.println(JSONObject.toJSON(map).toString());
//    }


}
